{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "2.2.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yananma/5_programs_per_day/blob/master/pytorch/ch2/2_2.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8hy0wxPsCqbE",
        "colab_type": "text"
      },
      "source": [
        "## 2.2 数据操作"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "mBVxa4ULDxWz",
        "colab_type": "text"
      },
      "source": [
        "### 2.2.1 创建 Tensor"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Khd9m0AnT1eL",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# 特别注明：任何可以改变tensor内容的操作都会在方法名后加一个下划线'_'\n",
        "# 例如：x.copy_(y), x.t_(), 这俩都会改变x的值。会用结果替换原来变量"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "CTmTMtRGD4LZ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "N2DCokUTD6g0",
        "colab_type": "code",
        "outputId": "4937a2ae-c05e-4b44-d0fe-5581c25944af",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "x = torch.arange(12)\n",
        "x"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 13
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4w8aZMJUD_Va",
        "colab_type": "code",
        "outputId": "ee0693e5-f7db-40d8-a8d4-eb16b5f5fdf3",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "x.shape"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([12])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TGwoyRUJFKOY",
        "colab_type": "code",
        "outputId": "e6c1a42a-3901-4b5b-f347-d7d240ddc117",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "x.shape[0]"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "12"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1xs0ftRLFU-i",
        "colab_type": "code",
        "outputId": "66ab7cbe-b084-4e53-944a-0c64814db97d",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "x.size()    # 和 shape 一样"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([12])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 23
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "iI6fduoZFWxe",
        "colab_type": "code",
        "outputId": "f8b13c83-aa0a-4064-aaf1-b5ca5a7086a1",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        }
      },
      "source": [
        "X = x.view(3, 4)\n",
        "X"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0,  1,  2,  3],\n",
              "        [ 4,  5,  6,  7],\n",
              "        [ 8,  9, 10, 11]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9C2jHEkAFZjl",
        "colab_type": "code",
        "outputId": "78509399-1c62-454c-9157-9ec931927e41",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 134
        }
      },
      "source": [
        "torch.zeros(2, 3, 4)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[[0., 0., 0., 0.],\n",
              "         [0., 0., 0., 0.],\n",
              "         [0., 0., 0., 0.]],\n",
              "\n",
              "        [[0., 0., 0., 0.],\n",
              "         [0., 0., 0., 0.],\n",
              "         [0., 0., 0., 0.]]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WIAip5fsFipV",
        "colab_type": "code",
        "outputId": "9653b014-fcc2-4394-da3b-7e11b1f5b2b3",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        }
      },
      "source": [
        "torch.ones(3, 4)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 1., 1., 1.],\n",
              "        [1., 1., 1., 1.],\n",
              "        [1., 1., 1., 1.]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vqxHgltvGT3h",
        "colab_type": "code",
        "outputId": "59f25c51-7ccb-4ba5-e85f-47c927a62f08",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        }
      },
      "source": [
        "Y = torch.tensor([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])\n",
        "Y"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[2, 1, 4, 3],\n",
              "        [1, 2, 3, 4],\n",
              "        [4, 3, 2, 1]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "lHI04kprGfuO",
        "colab_type": "code",
        "outputId": "94f2c045-3e21-4309-d1da-e71e0afd774f",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        }
      },
      "source": [
        "torch.randn(3, 4)    # 正态分布"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 1.1208,  1.3648,  0.4399, -0.1687],\n",
              "        [ 1.1830, -0.8392,  1.7999, -0.5877],\n",
              "        [ 0.2960, -1.1753,  0.8036, -0.5075]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-Du8f0JRGjPl",
        "colab_type": "text"
      },
      "source": [
        "### 2.2.2 运算"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0opksXIzGrtq",
        "colab_type": "code",
        "outputId": "a9587daa-af1e-47f7-c22a-abb3850add21",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        }
      },
      "source": [
        "X + Y "
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 2,  2,  6,  6],\n",
              "        [ 5,  7,  9, 11],\n",
              "        [12, 12, 12, 12]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 24
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5227Mi3nHEBf",
        "colab_type": "code",
        "outputId": "0508bf3a-812b-4432-8bfe-15e2ded3186a",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        }
      },
      "source": [
        "X * Y "
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0,  1,  8,  9],\n",
              "        [ 4, 10, 18, 28],\n",
              "        [32, 27, 20, 11]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 25
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "icXnxtuMHFih",
        "colab_type": "code",
        "outputId": "c4574eca-6683-4055-ff8a-3583534d6b4e",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        }
      },
      "source": [
        "X / Y"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0,  1,  0,  1],\n",
              "        [ 4,  2,  2,  1],\n",
              "        [ 2,  3,  5, 11]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 27
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mcF0L-Y4HHl8",
        "colab_type": "code",
        "outputId": "3e1ac7c2-c717-467a-f6d7-5fed892f013d",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        }
      },
      "source": [
        "torch.exp(Y.float())"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 7.3891,  2.7183, 54.5981, 20.0855],\n",
              "        [ 2.7183,  7.3891, 20.0855, 54.5981],\n",
              "        [54.5981, 20.0855,  7.3891,  2.7183]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 29
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "YuvWIC7iHbNa",
        "colab_type": "code",
        "outputId": "996e241e-404b-448e-b7ed-f845ed408505",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        }
      },
      "source": [
        "torch.mm(X, Y.T)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 18,  20,  10],\n",
              "        [ 58,  60,  50],\n",
              "        [ 98, 100,  90]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 30
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "RSKYo-0AHfFB",
        "colab_type": "code",
        "outputId": "9e365453-3819-4472-b47e-cad3036b8a91",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 118
        }
      },
      "source": [
        "torch.cat((X, Y), 0)    # 0 往下放，地下室，0 楼，纵向"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0,  1,  2,  3],\n",
              "        [ 4,  5,  6,  7],\n",
              "        [ 8,  9, 10, 11],\n",
              "        [ 2,  1,  4,  3],\n",
              "        [ 1,  2,  3,  4],\n",
              "        [ 4,  3,  2,  1]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 32
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "frmdzTWBHtlZ",
        "colab_type": "code",
        "outputId": "c2814cc5-226b-414f-a9b5-0ba145c3ba1e",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        }
      },
      "source": [
        "torch.cat((X, Y), 1)    # 1 平着并排放，1 楼，横向"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0,  1,  2,  3,  2,  1,  4,  3],\n",
              "        [ 4,  5,  6,  7,  1,  2,  3,  4],\n",
              "        [ 8,  9, 10, 11,  4,  3,  2,  1]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 33
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rIQPQ5E9IP2Q",
        "colab_type": "code",
        "outputId": "fe811d26-81f7-463a-8941-7e73bc9f3871",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        }
      },
      "source": [
        "(X == Y)  "
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[False,  True, False,  True],\n",
              "        [False, False, False, False],\n",
              "        [False, False, False, False]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 34
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ySvh8UZ3IVT3",
        "colab_type": "code",
        "outputId": "7fd58ae6-4bb2-459a-f8c7-f25c8b77b556",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        }
      },
      "source": [
        "(X == Y).int()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0, 1, 0, 1],\n",
              "        [0, 0, 0, 0],\n",
              "        [0, 0, 0, 0]], dtype=torch.int32)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 35
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Fv6XOJxNIY12",
        "colab_type": "code",
        "outputId": "e69ef94b-70fc-469e-fbeb-1fb1fc129ad0",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "X.sum()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(66)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 36
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wDDh7oKUIbQu",
        "colab_type": "code",
        "outputId": "b682fd20-887a-44f5-8f43-4dc6203300d6",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "X.float().norm().item()    # norm() L2 范数，每一项平方，所有项求和，开根号；.item() 只能转换一个数。"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "22.494443893432617"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 37
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "TnVccAzyI_8b",
        "colab_type": "text"
      },
      "source": [
        "### 2.2.3 广播机制"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ibFjP_21JEbU",
        "colab_type": "code",
        "outputId": "6af6057d-bcd8-49db-e8aa-1c4d2c531d68",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        }
      },
      "source": [
        "A = torch.arange(3).view(3, 1)\n",
        "A"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0],\n",
              "        [1],\n",
              "        [2]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 38
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8LF7cvpFJKm0",
        "colab_type": "code",
        "outputId": "353a2fe1-bbab-4876-90b2-e775062993ea",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "B = torch.arange(2).view(1, 2)\n",
        "B"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0, 1]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 39
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "u0aj3VUbJQPM",
        "colab_type": "code",
        "outputId": "cf416d04-3395-4336-c2dd-a1137ea39f67",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        }
      },
      "source": [
        "A + B"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0, 1],\n",
              "        [1, 2],\n",
              "        [2, 3]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 40
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "PeAOvMjEJTqN",
        "colab_type": "text"
      },
      "source": [
        "### 2.2.4 索引"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fhTkdvyZJZqT",
        "colab_type": "code",
        "outputId": "ac6399b3-fa87-445e-e51c-424b2b403575",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        }
      },
      "source": [
        "X[1:3]"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 4,  5,  6,  7],\n",
              "        [ 8,  9, 10, 11]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 41
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2KJ6yRIFJ5Tb",
        "colab_type": "code",
        "outputId": "20c75c6a-bd9a-440b-df9e-c458eafa48fb",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        }
      },
      "source": [
        "X[1, 2] = 9\n",
        "X"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0,  1,  2,  3],\n",
              "        [ 4,  5,  9,  7],\n",
              "        [ 8,  9, 10, 11]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 42
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GBizm4tvJ81k",
        "colab_type": "code",
        "outputId": "1ba34ff1-b966-4a6d-dd60-d98e78c76bd5",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        }
      },
      "source": [
        "X[1:2, :] = 12     # 第二层，所有列\n",
        "X"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0,  1,  2,  3],\n",
              "        [12, 12, 12, 12],\n",
              "        [ 8,  9, 10, 11]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 43
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GRyd6MToKJQb",
        "colab_type": "text"
      },
      "source": [
        "### 2.2.5 运算的内存开销"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "D3KCmUbbKZrY",
        "colab_type": "code",
        "outputId": "edef8c84-7f20-4105-ecd1-401b1220e3d4",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "before = id(Y)\n",
        "Y = Y + X \n",
        "id(Y) == before "
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 44
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Kr93OyqPKhWa",
        "colab_type": "code",
        "outputId": "fcb6fb89-f433-42ed-85c7-b2b8bec8ee4d",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "Z = torch.zeros_like(Y)\n",
        "before = id(Z)\n",
        "Z[:] = X + Y\n",
        "id(Z) == before "
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 45
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vO0hv8_eKsOv",
        "colab_type": "code",
        "outputId": "e0a02df2-b32f-4b11-cf43-d1b2301ac1fa",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "torch.add(X, Y, out=Z)\n",
        "id(Z) == before "
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 46
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-0N_uI0aK1GT",
        "colab_type": "code",
        "outputId": "fe90a5a9-2f1a-4061-af86-3cab3a758940",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "before = id(X)\n",
        "X += Y \n",
        "id(X) == before "
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 47
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9L2kfp_lK6r4",
        "colab_type": "text"
      },
      "source": [
        "### 2.2.6 Tensor 和 Numpy 互相转换"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vTsdzM67LGvQ",
        "colab_type": "code",
        "outputId": "a1917a15-870b-48d5-afb6-73578a4a3023",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        }
      },
      "source": [
        "import numpy as np \n",
        "\n",
        "P = np.ones((2, 3))\n",
        "T = torch.tensor(P)\n",
        "T"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 1., 1.],\n",
              "        [1., 1., 1.]], dtype=torch.float64)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 48
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "j6tVSNkDLbEf",
        "colab_type": "code",
        "outputId": "a03551e5-edb0-4b36-8887-56ce24f5b4c0",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        }
      },
      "source": [
        "T1 = torch.from_numpy(P)    # 从后往前念，P from numpy，转化为 torch 的 Tensor\n",
        "T1"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 1., 1.],\n",
              "        [1., 1., 1.]], dtype=torch.float64)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 49
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xOY6kOITLxu_",
        "colab_type": "code",
        "outputId": "d15e7cf4-b1a9-474f-8dee-01e11454d55d",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        }
      },
      "source": [
        "T.numpy()"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[1., 1., 1.],\n",
              "       [1., 1., 1.]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 50
        }
      ]
    }
  ]
}
